package fr.upmc.tps.mdoc.servlets;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.upmc.tps.mdoc.domain.DAOContact;
import fr.upmc.tps.mdoc.domain.DAOContactGroup;
import fr.upmc.tps.mdoc.domain.IDAOContact;
import fr.upmc.tps.mdoc.domain.IDAOContactGroup;
import fr.upmc.tps.mdoc.models.Entreprise;
import fr.upmc.tps.mdoc.models.IContact;
import fr.upmc.tps.mdoc.models.IContactGroup;
import fr.upmc.tps.mdoc.models.PhoneNumber;

/**
 * Servlet implementation class Modifier
 */
public class EditContact extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public EditContact() {
		super();
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		long id = Long.parseLong(request.getParameter("id"));

		request.getRequestDispatcher("pages/editContact.jsp?id=" + id).forward(
				request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		IDAOContact dao = new DAOContact();

		int contactId = Integer.parseInt(request.getParameter("id"));

		IContact contact;

		if (dao.getEntreprise(contactId) != null) {
		
			contact = (Entreprise) dao.getContact(contactId);
			((Entreprise)contact).setNumSiret(Integer.parseInt(request.getParameter("numSiret")));
		
		} else {
			
			contact = dao.getContact(contactId);
			
		}
		
		contact.setFirstName(request.getParameter("firstName"));
		contact.setLastName(request.getParameter("lastName"));
		contact.setEmail(request.getParameter("email"));

		contact.getAddress().setStreet(request.getParameter("street"));
		contact.getAddress().setCity(request.getParameter("city"));
		contact.getAddress().setZip(request.getParameter("zip"));
		contact.getAddress().setCountry(request.getParameter("country"));

		Set<PhoneNumber> pns = contact.getPhones();
		
		
		for(PhoneNumber pn : pns) {
		
			if(pn.getPhoneKind().equals("fixe"))
				pn.setPhoneNumber(request.getParameter("phoneFixe"));
			if(pn.getPhoneKind().equals("port"))
				pn.setPhoneNumber(request.getParameter("phonePort"));
			if(pn.getPhoneKind().equals("bureau"))
				pn.setPhoneNumber(request.getParameter("phoneOffice"));
			
		}
		

		// Récupération de cases cochés par l'utilisateur
		String groupnames[] = request.getParameterValues("groupname");
		//Set<ContactGroup> groups = new HashSet<ContactGroup>();
		contact.getBooks().clear();
		IDAOContactGroup daog = new DAOContactGroup();
		List<IContactGroup> groups = daog.getGroups();
		
		for (String g : Arrays.asList(groupnames)) {
			for (IContactGroup cg : groups)
				if (Integer.parseInt(g) == cg.getGroupId())
					contact.getBooks().add(cg);
		}

		//contact.setBooks(groups);
		
		if (dao.updateContact(contact)) {

			RequestDispatcher rd = request
					.getRequestDispatcher("pages/accueil.jsp");

			rd.forward(request, response);
		}

	}

}
